package com.platform.task;

import com.platform.entity.OrderVo;
import com.platform.service.ApiOrderService;
import com.platform.utils.DateUtils;
import com.platform.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component("orderTask")
public class OrderTask {
    @Autowired
    private ApiOrderService apiOrderService;

    public void cancelOrder(){
        //每分钟查询超时订单
        List<OrderVo> orderVos =apiOrderService.queryFutureOverTimeOrder();
        //超时则关闭
        if (orderVos!=null && orderVos.size()!=0){
            for (OrderVo orderVo: orderVos) {
                //final_pay_time是后来新增的字段
                //如果是空 则在add_time上新增两个小时
                if (StringUtils.isNullOrEmpty(orderVo.getFinalPayTime())){
                    orderVo.setFinalPayTime(DateUtils.addHour(orderVo.getAdd_time(),48));
                }
                //订单最终支付时间大于现在则关闭订单
                if (DateUtils.compareDate(orderVo.getFinalPayTime(),new Date())==1){
                    orderVo.setOrder_status(101);
                    apiOrderService.update(orderVo);
                }
            }
        }
    }
}
